<?
include("db/db.php");
#include "_libs/functions.php";

function checkproxy($host,$port)
{
	$url="http://www.itva.ru";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
	curl_setopt($ch, CURLOPT_URL, $url);
//	curl_setopt($ch, CURLOPT_POST, true);
//	curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
	curl_setopt($ch,CURLOPT_REFERER,$url);
	// curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
	curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
	//  curl_setopt( $ch, CURLOPT_ENCODING, "" );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
	curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
	curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );


	//$socks = $db->selectCell("select ip from ip order by rand()");
//	$portz = "65534";
	$proxy_type = "CURLPROXY_HTTP";

	
	@curl_setopt($ch, CURLOPT_PROXY, "$host:$port");
	//IP ????? ? ???? SOCKS5
	@curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type);
	curl_setopt( $ch, CURLOPT_TIMEOUT, 15);
	curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );

//	curl_setopt($ch, CURLOPT_POSTFIELDS, $pp);
	//echo "testing $host:$port ... ";

	$dta = curl_exec($ch);
	echo strlen($dta)." ";
	if(!strlen($dta)) {echo "dead";return false;}
	$info = curl_getinfo( $ch );
	//print_r($info);
	if(strstr($dta,"ITVA")) 
		return true;
	echo "bad";
	return false;
}

$i = 11;
$nump=0;
$url ="http://workingproxies.org/?page=$i";

$proxy_lists = array("http://agat.net.ru/many/proxy.txt","http://multiproxy.org/txt_all/proxy.txt","http://multiproxy.org/txt_anon/proxy.txt",
"http://searchlores.org/proxitk1.txt","http://searchlores.org/proxitk2.txt","http://searchlores.org/proxitk3.txt","http://searchlores.org/proxitk4.txt","http://searchlores.org/proxitk5.txt",
"http://searchlores.org/pxylist1.txt","http://searchlores.org/pxylist2.txt");


$url="http://nntime.com/";
$d=file_get_contents($url);
preg_match_all("#href=\"(/.*?)\"#msi",$d,$m,PREG_SET_ORDER);
$alr=array();
for($i=1;$i<31;$i++) 
//foreach($m as $u)
{
	$url="http://nntime.com/".$u[1];
//	$url ="http://www.samair.ru/proxy/ip-address-".sprintf("%02d", $i).".htm";
//	$url ="http://www.publicproxyservers.com/proxy/list$i.html";
//	$url="http://aliveproxy.com/fr-proxy-list/";
	echo "getting $url ... ";
	$file="postok/workingproxies_$i.html";
	$data[$i]=file_get_contents($url);
	$hash=md5($data[$i]);

	echo strlen($data[$i])." [$hash] ";
	if(!strlen($data[$i]))
		continue;
	//$m=array();
	//echo $data;
	unset($n);
	$n=preg_match_all("#<tr>\s+<td>(\d+\.\d+\.\d+\.\d+)</td>\s+<td>(\d+)</td>#msi",$data[$i],$m,PREG_SET_ORDER);
	if(!$n)
			$n=preg_match_all("#(\d+\.\d+\.\d+\.\d+):(\d+)#msi",$data[$i],$m,PREG_SET_ORDER);
	if(!$n)
		$n=preg_match_all("#<td\s+class=\"pthtdd\"><a\s+target=\"_blank\"\s+href=\"http://.*?\">(.*?)</a>.*?\"pthtd\">(\d+)</td>#msi",$data[$i],$m,PREG_SET_ORDER);
	echo " $n proxes\r\n";
	
	unlink($file);
	file_put_contents($file,$data[$i]);

	foreach($m as $ma)
	{	
		$nump++;
		$ip=$ma[1];
		$port=$ma[2];
		echo "\r\n#$nump $ip:$port ";
		$e=$rdb->selectCell("select id from proxy.ip where ip=?",$ip,$port);
		if($e)
		{
			echo "already exist $e\r\n";
			continue;
		}
		
//		$ret=checkproxy($ip,$port);
//		if(!$ret) {
//			//echo " bad\r\n";
//			continue;
//		}
		$type='unknown';
		$id = $rdb->query("insert into proxy.ip set ip=?,status=0,port=?,type=?",
			$ip,$port,$type);
		echo $id."\r\n";
	}
	//exit(0);
}
?>
